{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "### 常用变量和参数名"
      ],
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from loan import *"
      ],
      "outputs": [],
      "execution_count": 1,
      "metadata": {
        "execution": {
          "iopub.execute_input": "2022-11-13T11:55:14.416Z",
          "iopub.status.busy": "2022-11-13T11:55:14.409Z",
          "iopub.status.idle": "2022-11-13T11:55:14.428Z",
          "shell.execute_reply": "2022-11-13T11:55:14.434Z"
        },
        "jupyter": {
          "outputs_hidden": false,
          "source_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "loan = Loan000(principal=1200, terms=6, yearStart=2022, monthStart=2, dayStart=10, dayRepay=10, rateDaily=0.0004)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "TOP 1.0746189516997906\n",
            "TOP * PRINCIPLE 1289.5427420397486\n",
            "TOP 1.0746189516997906\n",
            "BOTTOM 6.186618775303289\n",
            "主键：094f8b52-634a-11ed-8559-8c8590a07d4b\n",
            "本金：1200\n",
            "贷款期次：6\n",
            "起息日：2022/2/2022\n",
            "每月固定还款日：10\n",
            "左闭右开区间，算头不算尾：2022-02-10 2022-03-10：28\n",
            "左闭右开区间，算头不算尾：2022-03-10 2022-04-10：31\n",
            "左闭右开区间，算头不算尾：2022-04-10 2022-05-10：30\n",
            "左闭右开区间，算头不算尾：2022-05-10 2022-06-10：31\n",
            "左闭右开区间，算头不算尾：2022-06-10 2022-07-10：30\n",
            "左闭右开区间，算头不算尾：2022-07-10 2022-08-10：31\n",
            "确定起息日、还款日：\n",
            "['2022-02-10', '2022-03-10', '2022-04-10', '2022-05-10', '2022-06-10', '2022-07-10', '2022-08-10']\n",
            "确定每期天数（算头不算尾）：\n",
            "[28, 31, 30, 31, 30, 31]\n",
            "确定等额本息每期应还金额：208.44\n",
            "期次：1\n",
            "本期应还利息：13.44\n",
            "本期应还本金：195.0\n",
            "期初应还本金：1200\n",
            "期末应还本金：1005.0\n",
            "期次：2\n",
            "本期应还利息：12.46\n",
            "本期应还本金：195.98\n",
            "期初应还本金：1005.0\n",
            "期末应还本金：809.02\n",
            "期次：3\n",
            "本期应还利息：9.7\n",
            "本期应还本金：198.74\n",
            "期初应还本金：809.02\n",
            "期末应还本金：610.28\n",
            "期次：4\n",
            "本期应还利息：7.56\n",
            "本期应还本金：200.88\n",
            "期初应还本金：610.28\n",
            "期末应还本金：409.4\n",
            "期次：5\n",
            "本期应还利息：4.91\n",
            "本期应还本金：203.53\n",
            "期初应还本金：409.4\n",
            "期末应还本金：205.87\n",
            "期次：6\n",
            "本期应还利息：2.55\n",
            "本期应还本金：205.87\n",
            "期初应还本金：205.87\n",
            "期末应还本金：0\n"
          ]
        }
      ],
      "execution_count": 2,
      "metadata": {
        "execution": {
          "iopub.status.busy": "2022-11-13T11:55:15.361Z",
          "iopub.execute_input": "2022-11-13T11:55:15.369Z",
          "iopub.status.idle": "2022-11-13T11:55:15.398Z",
          "shell.execute_reply": "2022-11-13T11:55:15.405Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "loanAdmin.previewRepayment()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "主键：b30a1f60-573b-11ed-a9f3-8c8590a07d4b\n",
            "本金：1200\n",
            "贷款期次：6\n",
            "起息日：2022/2/10\n",
            "每月固定还款日：1\n",
            "左闭右开区间，算头不算尾：2022-02-10 2022-03-01：19\n",
            "左闭右开区间，算头不算尾：2022-03-01 2022-04-01：31\n",
            "左闭右开区间，算头不算尾：2022-04-01 2022-05-01：30\n",
            "左闭右开区间，算头不算尾：2022-05-01 2022-06-01：31\n",
            "左闭右开区间，算头不算尾：2022-06-01 2022-07-01：30\n",
            "左闭右开区间，算头不算尾：2022-07-01 2022-08-01：31\n",
            "确定起息日、还款日：\n",
            "['2022-02-10', '2022-03-01', '2022-04-01', '2022-05-01', '2022-06-01', '2022-07-01', '2022-08-01']\n",
            "确定每期天数（算头不算尾）：\n",
            "[19, 31, 30, 31, 30, 31]\n",
            "确定等额本息每期应还金额：207.69\n",
            "期次：1\n",
            "本期应还利息：9.12\n",
            "本期应还本金：198.57\n",
            "期初应还本金：1200\n",
            "期末应还本金：1001.43\n",
            "期次：2\n",
            "本期应还利息：12.41\n",
            "本期应还本金：195.28\n",
            "期初应还本金：1001.43\n",
            "期末应还本金：806.15\n",
            "期次：3\n",
            "本期应还利息：9.67\n",
            "本期应还本金：198.02\n",
            "期初应还本金：806.15\n",
            "期末应还本金：608.13\n",
            "期次：4\n",
            "本期应还利息：7.54\n",
            "本期应还本金：200.15\n",
            "期初应还本金：608.13\n",
            "期末应还本金：407.98\n",
            "期次：5\n",
            "本期应还利息：4.89\n",
            "本期应还本金：202.8\n",
            "期初应还本金：407.98\n",
            "期末应还本金：205.18\n",
            "期次：6\n",
            "本期应还利息：2.54\n",
            "本期应还本金：205.18\n",
            "期初应还本金：205.18\n",
            "期末应还本金：0\n"
          ]
        }
      ],
      "execution_count": 3,
      "metadata": {
        "execution": {
          "iopub.status.busy": "2022-10-29T03:42:24.709Z",
          "iopub.execute_input": "2022-10-29T03:42:24.716Z",
          "iopub.status.idle": "2022-10-29T03:42:24.734Z",
          "shell.execute_reply": "2022-10-29T03:42:24.741Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 11,
          "data": {
            "text/plain": "0"
          },
          "metadata": {}
        }
      ],
      "execution_count": 11,
      "metadata": {
        "execution": {
          "iopub.status.busy": "2022-10-28T14:43:07.470Z",
          "iopub.execute_input": "2022-10-28T14:43:07.478Z",
          "iopub.status.idle": "2022-10-28T14:43:07.495Z",
          "shell.execute_reply": "2022-10-28T14:43:07.504Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 6,
          "data": {
            "text/plain": "205.86999999999998"
          },
          "metadata": {}
        }
      ],
      "execution_count": 6,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [],
      "execution_count": 7,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "x = Decimal(str(409.4))\n",
        "y = Decimal(str(203.53))\n",
        "float(x - y)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 12,
          "data": {
            "text/plain": "205.87"
          },
          "metadata": {}
        }
      ],
      "execution_count": 12,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [],
      "execution_count": null,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[datetime.datetime(2022, 2, 10, 0, 0), datetime.datetime(2022, 3, 10, 0, 0), datetime.datetime(2022, 4, 10, 0, 0), datetime.datetime(2022, 5, 10, 0, 0), datetime.datetime(2022, 6, 10, 0, 0), datetime.datetime(2022, 7, 10, 0, 0), datetime.datetime(2022, 8, 10, 0, 0)]\n",
            "[28, 31, 30, 31, 30, 31]\n"
          ]
        }
      ],
      "execution_count": 4,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "INFO:root:123\n",
            "DEBUG:root:123\n"
          ]
        }
      ],
      "execution_count": 1,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [],
      "execution_count": null,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "28 days, 0:00:00\n",
            "31 days, 0:00:00\n"
          ]
        },
        {
          "output_type": "execute_result",
          "execution_count": 8,
          "data": {
            "text/plain": "1"
          },
          "metadata": {}
        }
      ],
      "execution_count": 8,
      "metadata": {
        "jupyter": {
          "outputs_hidden": false,
          "source_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1\n",
            "2\n",
            "3\n"
          ]
        }
      ],
      "execution_count": 10,
      "metadata": {
        "jupyter": {
          "outputs_hidden": false,
          "source_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 还款计划试算"
      ],
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import math\n",
        "#金额单位“分”\n",
        "#年利率百分\n",
        "#月利率千分\n",
        "#日利率万分，一厘一毫，“角”、“分”、“厘”、“毫”\n",
        "principal=100000\n",
        "terms=6\n",
        "instalment_corpus_accrued=100000//6\n",
        "\n",
        "\n",
        "def caculate_instalment_corpus_accrued(principal, terms, monthlyRate):\n",
        "  instalment_corpus_accrued = principal * monthlyRate * ( 1 + monthlyRate ) ** terms / ( ( 1 + monthlyRate ) ** terms - 1 )\n",
        "  return instalment_corpus_accrued\n",
        "\n",
        "  \n",
        "def caculate_plan_replay(n, terms, principal, monthlyRate):\n",
        "  \n",
        "  if n == 1:\n",
        "    \n",
        "    plan = []\n",
        "    \n",
        "    last_contract_principal_accrued = 0\n",
        "    instalment_corpus_accrued = caculate_instalment_corpus_accrued(principal, terms, monthlyRate)\n",
        "    instalment_interest_accrued = principal * 1 * monthlyRate\n",
        "    instalment_principal_accrued = instalment_corpus_accrued - instalment_interest_accrued\n",
        "    contract_principal_accrued = last_contract_principal_accrued  + instalment_principal_accrued\n",
        "    contract_interest_accrued = instalment_corpus_accrued * n - contract_principal_accrued\n",
        "    #contract_principal_accrued = int( contract_principal_accrued * amountPrecision ) / amountPrecision\n",
        "    \n",
        "    \n",
        "    #amounts = [ \n",
        "    #  contract_principal_accrued, \n",
        "    #  contract_interest_accrued, \n",
        "    #  instalment_corpus_accrued, \n",
        "    #  instalment_principal_accrued, \n",
        "    #  instalment_interest_accrued \n",
        "    #]    \n",
        "    amounts = {\n",
        "      'term': n,\n",
        "      'contract_principal_accrued': contract_principal_accrued, \n",
        "      'contract_interest_accrued': contract_interest_accrued, \n",
        "      'instalment_corpus_accrued': instalment_corpus_accrued, \n",
        "      'instalment_principal_accrued': instalment_principal_accrued, \n",
        "      'instalment_interest_accrued': instalment_interest_accrued \n",
        "    }\n",
        "    \n",
        "    #print(\"Terms: {0};\".format(n))\n",
        "    #print(\"Contract Principal Accrued: {0};\".format(contract_principal_accrued))\n",
        "    #print(\"Contract Interest Accrued: {0};\".format(contract_interest_accrued))\n",
        "    #print(\"Instalment Corpus Accrued: {0};\".format(instalment_corpus_accrued))\n",
        "    #print(\"Instalment Principal Accrued: {0};\".format(instalment_principal_accrued))\n",
        "    #print(\"Instalment Interest Accrued: {0};\".format(instalment_interest_accrued))\n",
        "    \n",
        "    #print(\"Terms: {0};\".format(n))\n",
        "    #print(\"Contract Principal Accrued: {0};\".format(amounts[0]))\n",
        "    #print(\"Contract Interest Accrued: {0};\".format(amounts[1]))\n",
        "    #print(\"Instalment Corpus Accrued: {0};\".format(amounts[2]))\n",
        "    #print(\"Instalment Principal Accrued: {0};\".format(amounts[3]))\n",
        "    #print(\"Instalment Interest Accrued: {0};\".format(amounts[4]))\n",
        "    \n",
        "    print(\"Terms: {0};\".format(amounts['term']))\n",
        "    print(\"Contract Principal Accrued: {0};\".format(amounts['contract_principal_accrued']))\n",
        "    print(\"Contract Interest Accrued: {0};\".format(amounts['contract_interest_accrued']))\n",
        "    print(\"Instalment Corpus Accrued: {0};\".format(amounts['instalment_corpus_accrued']))\n",
        "    print(\"Instalment Principal Accrued: {0};\".format(amounts['instalment_principal_accrued']))\n",
        "    print(\"Instalment Interest Accrued: {0};\".format(amounts['instalment_interest_accrued']))\n",
        "    \n",
        "    plan.append(amounts)\n",
        "\n",
        "    return plan\n",
        "  \n",
        "  plan = caculate_plan_replay(n-1, terms, principal, monthlyRate)\n",
        "  last_contract_principal_accrued = plan[-1]['contract_principal_accrued']\n",
        "  instalment_corpus_accrued = caculate_instalment_corpus_accrued(principal, terms, monthlyRate)\n",
        "  instalment_interest_accrued = (principal - last_contract_principal_accrued) * 1 *  monthlyRate\n",
        "  instalment_principal_accrued = instalment_corpus_accrued - instalment_interest_accrued\n",
        "  contract_principal_accrued = last_contract_principal_accrued + instalment_principal_accrued\n",
        "  contract_interest_accrued = instalment_corpus_accrued * n - contract_principal_accrued\n",
        "  \n",
        "  #amounts = [ \n",
        "  #   contract_principal_accrued, \n",
        "  #   contract_interest_accrued, \n",
        "  #   instalment_corpus_accrued, \n",
        "  #   instalment_principal_accrued, \n",
        "  #   instalment_interest_accrued \n",
        "  #]\n",
        "  amounts = {\n",
        "    'term': n,\n",
        "    'contract_principal_accrued': contract_principal_accrued, \n",
        "    'contract_interest_accrued': contract_interest_accrued, \n",
        "    'instalment_corpus_accrued': instalment_corpus_accrued, \n",
        "    'instalment_principal_accrued': instalment_principal_accrued, \n",
        "    'instalment_interest_accrued': instalment_interest_accrued \n",
        "  }\n",
        "  \n",
        "  #print(\"Terms: {0};\".format(n))\n",
        "  #print(\"Contract Principal Accrued: {0};\".format(contract_principal_accrued))\n",
        "  #print(\"Contract Interest Accrued: {0};\".format(contract_interest_accrued))\n",
        "  #print(\"Instalment Corpus Accrued: {0};\".format(instalment_corpus_accrued))\n",
        "  #print(\"Instalment Principal Accrued: {0};\".format(instalment_principal_accrued))\n",
        "  #print(\"Instalment Interest Accrued: {0};\".format(instalment_interest_accrued))\n",
        "  \n",
        "  #print(\"Terms: {0};\".format(n))\n",
        "  #print(\"Contract Principal Accrued: {0};\".format(amounts[0]))\n",
        "  #print(\"Contract Interest Accrued: {0};\".format(amounts[1]))\n",
        "  #print(\"Instalment Corpus Accrued: {0};\".format(amounts[2]))\n",
        "  #print(\"Instalment Principal Accrued: {0};\".format(amounts[3]))\n",
        "  #print(\"Instalment Interest Accrued: {0};\".format(amounts[4]))\n",
        "  \n",
        "  print(\"Terms: {0};\".format(amounts['term']))\n",
        "  print(\"Contract Principal Accrued: {0};\".format(amounts['contract_principal_accrued']))\n",
        "  print(\"Contract Interest Accrued: {0};\".format(amounts['contract_interest_accrued']))\n",
        "  print(\"Instalment Corpus Accrued: {0};\".format(amounts['instalment_corpus_accrued']))\n",
        "  print(\"Instalment Principal Accrued: {0};\".format(amounts['instalment_principal_accrued']))\n",
        "  print(\"Instalment Interest Accrued: {0};\".format(amounts['instalment_interest_accrued']))\n",
        "\n",
        "  plan.append(amounts)\n",
        "  \n",
        "  return plan\n",
        "\n",
        "amountPrecision=100\n",
        "principal=100\n",
        "term=3\n",
        "terms=3\n",
        "dailyRate=0.0004\n",
        "monthlyRate=dailyRate*30\n",
        "annualRate=monthlyRate*12\n",
        "annualRate=0.0435\n",
        "monthlyRate=annualRate / 12\n",
        "dailyRate=monthlyRate / 30\n",
        "\n",
        "plan = caculate_plan_replay(3, 3, 100 * 100, 0.0435 / 12)\n",
        "plan = caculate_plan_replay(6, 6, 1200 * 100, 0.0004 * 30)\n",
        "\n",
        "#计算输出\n",
        "planProcessed = [ [ round(obj[k])/100 for k in obj ] for obj in plan ]\n",
        "print(planProcessed)\n",
        "\n",
        "#等额本息，每期月供，月供向上取整，利息向下取整，本金=月供-利息，月供和利息都是计算得到，再由月供和利息得到本金，所以月供和利息规定取整方式\n",
        "#基本能和招行的计算器对上，但招行的计算器自身有问题，“月供=本金+利息”不一定成立\n",
        "for obj in plan:\n",
        "  obj['instalment_corpus_accrued']=round(int(obj['instalment_corpus_accrued']+1), 2)\n",
        "  obj['instalment_interest_accrued']=round(int(obj['instalment_interest_accrued']), 2)\n",
        "  obj['instalment_principal_accrued']=round(obj['instalment_corpus_accrued'] - obj['instalment_interest_accrued'], 2)\n",
        "planProcessed = [ [ round(obj[k])/100 for k in obj ] for obj in plan ]\n",
        "print(planProcessed)\n",
        "\n"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Terms: 1;\n",
            "Contract Principal Accrued: 3321.279148525333;\n",
            "Contract Interest Accrued: 36.25;\n",
            "Instalment Corpus Accrued: 3357.529148525333;\n",
            "Instalment Principal Accrued: 3321.279148525333;\n",
            "Instalment Interest Accrued: 36.25;\n",
            "Terms: 2;\n",
            "Contract Principal Accrued: 6654.59793396407;\n",
            "Contract Interest Accrued: 60.46036308659586;\n",
            "Instalment Corpus Accrued: 3357.529148525333;\n",
            "Instalment Principal Accrued: 3333.318785438737;\n",
            "Instalment Interest Accrued: 24.210363086595667;\n",
            "Terms: 3;\n",
            "Contract Principal Accrued: 10000.000000000022;\n",
            "Contract Interest Accrued: 72.58744557597674;\n",
            "Instalment Corpus Accrued: 3357.529148525333;\n",
            "Instalment Principal Accrued: 3345.4020660359524;\n",
            "Instalment Interest Accrued: 12.127082489380246;\n",
            "Terms: 1;\n",
            "Contract Principal Accrued: 19408.349267016078;\n",
            "Contract Interest Accrued: 1440.0;\n",
            "Instalment Corpus Accrued: 20848.349267016078;\n",
            "Instalment Principal Accrued: 19408.349267016078;\n",
            "Instalment Interest Accrued: 1440.0;\n",
            "Terms: 2;\n",
            "Contract Principal Accrued: 39049.59872523635;\n",
            "Contract Interest Accrued: 2647.099808795807;\n",
            "Instalment Corpus Accrued: 20848.349267016078;\n",
            "Instalment Principal Accrued: 19641.24945822027;\n",
            "Instalment Interest Accrued: 1207.0998087958071;\n",
            "Terms: 3;\n",
            "Contract Principal Accrued: 58926.54317695527;\n",
            "Contract Interest Accrued: 3618.504624092966;\n",
            "Instalment Corpus Accrued: 20848.349267016078;\n",
            "Instalment Principal Accrued: 19876.944451718915;\n",
            "Instalment Interest Accrued: 971.404815297164;\n",
            "Terms: 4;\n",
            "Contract Principal Accrued: 79042.0109620948;\n",
            "Contract Interest Accrued: 4351.386105969505;\n",
            "Instalment Corpus Accrued: 20848.349267016078;\n",
            "Instalment Principal Accrued: 20115.46778513954;\n",
            "Instalment Interest Accrued: 732.8814818765368;\n",
            "Terms: 5;\n",
            "Contract Principal Accrued: 99398.86436065602;\n",
            "Contract Interest Accrued: 4842.8819744243665;\n",
            "Instalment Corpus Accrued: 20848.349267016078;\n",
            "Instalment Principal Accrued: 20356.853398561216;\n",
            "Instalment Interest Accrued: 491.4958684548623;\n",
            "Terms: 6;\n",
            "Contract Principal Accrued: 119999.99999999997;\n",
            "Contract Interest Accrued: 5090.095602096495;\n",
            "Instalment Corpus Accrued: 20848.349267016078;\n",
            "Instalment Principal Accrued: 20601.13563934395;\n",
            "Instalment Interest Accrued: 247.21362767212776;\n",
            "[[0.01, 194.08, 14.4, 208.48, 194.08, 14.4], [0.02, 390.5, 26.47, 208.48, 196.41, 12.07], [0.03, 589.27, 36.19, 208.48, 198.77, 9.71], [0.04, 790.42, 43.51, 208.48, 201.15, 7.33], [0.05, 993.99, 48.43, 208.48, 203.57, 4.91], [0.06, 1200.0, 50.9, 208.48, 206.01, 2.47]]\n",
            "[[0.01, 194.08, 14.4, 208.49, 194.09, 14.4], [0.02, 390.5, 26.47, 208.49, 196.42, 12.07], [0.03, 589.27, 36.19, 208.49, 198.78, 9.71], [0.04, 790.42, 43.51, 208.49, 201.17, 7.32], [0.05, 993.99, 48.43, 208.49, 203.58, 4.91], [0.06, 1200.0, 50.9, 208.49, 206.02, 2.47]]\n"
          ]
        }
      ],
      "execution_count": 11,
      "metadata": {
        "collapsed": true,
        "execution": {
          "iopub.execute_input": "2022-08-17T12:27:08.805Z",
          "iopub.status.busy": "2022-08-17T12:27:08.801Z",
          "iopub.status.idle": "2022-08-17T12:27:08.815Z",
          "shell.execute_reply": "2022-08-17T12:27:08.819Z"
        },
        "jupyter": {
          "outputs_hidden": false,
          "source_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [
        {
          "output_type": "error",
          "ename": "SyntaxError",
          "evalue": "invalid character in identifier (<ipython-input-12-71f49f27d6a0>, line 1)",
          "traceback": [
            "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-12-71f49f27d6a0>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    （一）贷款未逾期的，按照先还利息、后还本金的顺序依次偿还；\u001b[0m\n\u001b[0m                                ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid character in identifier\n"
          ]
        }
      ],
      "execution_count": 12,
      "metadata": {
        "execution": {
          "iopub.execute_input": "2022-08-20T16:20:48.665Z",
          "iopub.status.busy": "2022-08-20T16:20:48.656Z"
        },
        "jupyter": {
          "outputs_hidden": false,
          "source_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "2022\n",
            "['2022-06-10 00:00:00', '2022-07-10 00:00:00', '2022-08-10 00:00:00', '2022-09-10 00:00:00', '2022-10-10 00:00:00', '2022-11-10 00:00:00']\n",
            "[30, 31, 31, 30, 31]\n",
            "[30, 31, 31, 30, 31]\n",
            "['2022-06-10 00:00:00', '2022-07-10 00:00:00', '2022-08-09 00:00:00', '2022-09-08 00:00:00', '2022-10-08 00:00:00', '2022-11-07 00:00:00', '2022-12-07 00:00:00']\n",
            "[30, 30, 30, 30, 30, 30]\n"
          ]
        }
      ],
      "execution_count": 35,
      "metadata": {
        "collapsed": true,
        "execution": {
          "iopub.execute_input": "2022-08-21T15:00:51.710Z",
          "iopub.status.busy": "2022-08-21T15:00:51.706Z",
          "iopub.status.idle": "2022-08-21T15:00:51.720Z",
          "shell.execute_reply": "2022-08-21T15:00:51.723Z"
        },
        "jupyter": {
          "outputs_hidden": false,
          "source_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "31\n",
            "28 days, 0:00:00\n",
            "[1, 2, 3, 4, 5]\n",
            "[1, 3, 4, 5]\n"
          ]
        }
      ],
      "execution_count": 5,
      "metadata": {
        "execution": {
          "iopub.execute_input": "2022-08-21T14:49:09.258Z",
          "iopub.status.busy": "2022-08-21T14:49:09.254Z",
          "iopub.status.idle": "2022-08-21T14:49:09.268Z",
          "shell.execute_reply": "2022-08-21T14:49:09.272Z"
        },
        "jupyter": {
          "outputs_hidden": false,
          "source_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Qn 每期还款结束剩余本金\n",
        "P 本金\n",
        "r 日利率\n",
        "R 月利率\n",
        "Dn 每期实际天数\n",
        "X 每期等额本息，目标\n",
        "\n",
        "Q1 = P * ( 1 + D1 * r ) - X\n",
        "\n",
        "Q2 = Q1 * ( 1 + D2 * r) - X\n",
        "   = P * ( 1 + D1 * r ) * ( 1 + D2 * r) - ( 1 + D2 * r) * X - X\n",
        "   \n",
        "Q3 = Q2 * ( 1 + D3 * r) - X\n",
        "   = P * ( 1 + D1 * r ) * ( 1 + D2 * r) * ( 1 + D3 * r) - ( 1 + D2 * r) * ( 1 + D3 * r) * X - ( 1 + D3 * r) * X - X\n",
        "   \n",
        "Q4 = Q3 * ( 1 + D4 * r) - X\n",
        "   = P * ( 1 + D1 * r ) * ( 1 + D2 * r) * ( 1 + D3 * r) * ( 1 + D4 * r) - ( 1 + D2 * r) * ( 1 + D3 * r) * ( 1 + D4 * r) * X - ( 1 + D3 * r) * ( 1 + D4 * r) * X - ( 1 + D4 * r) * X - X\n",
        "\n",
        "\n",
        "Qn = \n",
        "\n",
        "\n",
        "if D1 = D2 = D3 = D4\n",
        "then R = D * r"
      ],
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\n",
        "Qn 每期还款结束剩余本金\n",
        "P 本金\n",
        "r 日利率\n",
        "R 月利率\n",
        "Dn 每期实际天数\n",
        "X 每期等额本息，目标\n",
        "\n",
        "Q1 = P * ( 1 + D1 * r ) - X\n",
        "Q2 = Q1 * ( 1 + D2 * r) - X\n",
        "   = P * ( 1 + D1 * r ) * ( 1 + D2 * r) - ( 1 + D2 * r) * X - X\n",
        "Q3 = Q2 * ( 1 + D3 * r) - X\n",
        "   = P * ( 1 + D1 * r ) * ( 1 + D2 * r) * ( 1 + D3 * r) - ( 1 + D2 * r) * ( 1 + D3 * r) * X - ( 1 + D3 * r) * X - X\n",
        "Q4 = Q3 * ( 1 + D4 * r) - X\n",
        "   = P * ( 1 + D1 * r ) * ( 1 + D2 * r) * ( 1 + D3 * r) * ( 1 + D4 * r) - ( 1 + D2 * r) * ( 1 + D3 * r) * ( 1 + D4 * r) * X - ( 1 + D3 * r) * ( 1 + D4 * r) * X - ( 1 + D4 * r) * X - X\n",
        "\n",
        "Qn = P * ( 1 + D1 * r ) * ( 1 + D2 * r) * ( 1 + D3 * r) * ... * ( 1 + Dn * r) - ( 1 + D2 * r) * ( 1 + D3 * r) * ... * ( 1 + Dn * r) * X - ( 1 + D3 * r) * ... * ( 1 + Dn * r) * X - ... - ( 1 + Dn * r) * X - X = 0\n",
        "\n",
        "P * ( 1 + D1 * r ) * ( 1 + D2 * r) * ( 1 + D3 * r) * ... * ( 1 + Dn * r) = ( 1 + D2 * r) * ( 1 + D3 * r) * ... * ( 1 + Dn * r) * X + ( 1 + D3 * r) * ... * ( 1 + Dn * r) * X + ... + ( 1 + Dn * r) * X + X\n",
        "\n",
        "X = (P * ( 1 + D1 * r ) * ( 1 + D2 * r) * ( 1 + D3 * r) * ... * ( 1 + Dn * r)) / (( 1 + D2 * r) * ( 1 + D3 * r) * ... * ( 1 + Dn * r) + ( 1 + D3 * r) * ... * ( 1 + Dn * r) + ... + ( 1 + Dn * r) + 1)\n",
        "\n",
        "\n",
        "\n",
        "if D1 = D2 = D3 = D4\n",
        "then R = D * r"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "outputs_hidden": false,
          "source_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "python3"
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.8.5",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    },
    "nteract": {
      "version": "0.28.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 1
}